package com.data.center.util.ForeignManage;
import cn.hutool.core.bean.BeanUtil;
import com.data.center.exception.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
/**
 * @author Li_Jx
 * @description: 通用的外键依赖查询工具类
 * @date 2023/11/18 19:24
 */
@Component
@Slf4j
public class ForeignDataUtil {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ForeignManageUtils foreignManageUtils;
    /**
     * @description 查询当前数据是否有被依赖
     * @param entity 需包含有效id
     * @return boolean
     * @author Li_Jx
     * @Date 2023/11/18 20:12
     */
    public boolean searchByForeignKey(Object entity) throws InvocationTargetException, IllegalAccessException {
        List<ForeignDataItem> foreignDataItems = foreignManageUtils.getForeignDataItem(entity.getClass().getName());

        for (ForeignDataItem item : foreignDataItems) {
            if (BeanUtil.getFieldValue(entity, item.foreignKey) == null){
                throw new BizException("查询外键关联失败");
            }
            List list = (List) item.searchByForeignKeyMethod.invoke(applicationContext.getBean((Class) item.serviceClass), BeanUtil.getFieldValue(entity, item.foreignKey));
            if (list.size() > 0){
                return false;
            }
        }
        return true;
    }
    /**
     * @description 将输入对象的所有关联数据删除（暂定）
     * @author Li_Jx
     * @Date 2023/11/18 20:23
     */
    public void deleteByForeignKey(Object entity) throws InvocationTargetException, IllegalAccessException {
        List<ForeignDataItem> foreignDataItems = foreignManageUtils.getForeignDataItem(entity.getClass().getName());

        for (ForeignDataItem item : foreignDataItems) {
            if (BeanUtil.getFieldValue(entity, item.foreignKey) == null){
                throw new BizException("查询外键关联失败");
            }
            List list = (List) item.searchByForeignKeyMethod.invoke(applicationContext.getBean((Class) item.serviceClass), BeanUtil.getFieldValue(entity, item.foreignKey));
            if (list != null && list.size() > 0){
                item.deleteByForeignKeyMethod.invoke(applicationContext.getBean((Class) item.serviceClass), BeanUtil.getFieldValue(entity,item.foreignKey));
            }
        }
    }

    /**
     * @description 对外键关联数据对象进行融合
     * @param entity(保留对象),deletedEntity(被删除对象)
     * @return 无返回值
     * @author Li_Jx
     * @Date 2023/11/18 20:39
     */
    public void mergeByForeignKey(Object entity,Object deletedEntity) throws InvocationTargetException, IllegalAccessException {
        List<ForeignDataItem> foreignDataItems = foreignManageUtils.getForeignDataItem(entity.getClass().getName());

        for (ForeignDataItem item : foreignDataItems) {
            if (BeanUtil.getFieldValue(entity, item.foreignKey) == null || BeanUtil.getFieldValue(deletedEntity, item.foreignKey) == null){
                throw new BizException("查询外键关联失败");
            }
            List list = (List) item.searchByForeignKeyMethod.invoke(applicationContext.getBean((Class) item.serviceClass), BeanUtil.getFieldValue(deletedEntity, item.foreignKey));
            if (list != null && list.size() > 0){
                item.mergeByForeignKeyMethod.invoke(applicationContext.getBean((Class) item.serviceClass), BeanUtil.getFieldValue(entity,item.foreignKey),BeanUtil.getFieldValue(deletedEntity,item.foreignKey));
            }
        }
    }
}